﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;

namespace mSpy.Plugins
{
    public static class Extensions
    {
        public static T To<T>(this DynamicEntity entity, string field)
        {
            if (entity != null)
            {
                if (entity.Properties.Contains(field))
                {
                    return ((T)entity.Properties[field]);
                }
            }

            return default(T);
        }

        public static string ConvertToString(this DynamicEntity entity, string field)
        {
            if (entity != null)
            {
                if (entity.Properties.Contains(field))
                {
                    Type propertyType = entity.Properties[field].GetType();
                    if (propertyType == typeof(string))
                    {
                        return entity.To<string>(field);
                    } /* begin lookup/guid conversion */
                    else if (propertyType == typeof(UniqueIdentifier))
                    {
                        return entity.To<UniqueIdentifier>(field).Value.ToString();
                    }
                    else if (propertyType == typeof(Lookup))
                    {
                        var o = entity.To<Lookup>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(Customer))
                    {
                        Customer c = entity.To<Customer>(field);
                        return c != null ? c.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(Key))
                    {
                        return entity.To<Key>(field).Value.ToString();
                    }
                    else if (propertyType == typeof(Owner))
                    {
                        var o = entity.To<Owner>(field);
                        return o != null ? o.Value.ToString() : "";
                    } /* end lookup/guid version */
                    else if (propertyType == typeof(CrmDateTime))
                    {
                        var o = entity.To<CrmDateTime>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(CrmNumber))
                    {
                        var o = entity.To<CrmNumber>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(CrmMoney))
                    {
                        var o = entity.To<CrmMoney>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(CrmDecimal))
                    {
                        var o = entity.To<CrmDecimal>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(CrmFloat))
                    {
                        var o = entity.To<CrmFloat>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(Picklist))
                    {
                        var o = entity.To<Picklist>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(Status))
                    {
                        var o = entity.To<Status>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                    else if (propertyType == typeof(CrmBoolean))
                    {
                        var o = entity.To<CrmBoolean>(field);
                        return o != null ? o.Value.ToString() : "";
                    }
                }
            }
            return "";
        }
    }
}
